package xiang.weiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xiang.weiyu.common.BaseContext;
import xiang.weiyu.common.R;
import xiang.weiyu.pojo.AddressBook;
import xiang.weiyu.service.AddressBookService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return R<String>
     */
    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook) {
        //1.将所有的地址都设置为不是默认/0
        LambdaUpdateWrapper<AddressBook> uw = new LambdaUpdateWrapper<>();
        uw.set(AddressBook::getIsDefault, 0).eq(AddressBook::getUserId, BaseContext.getCurrentId());// where user_id =?
        addressBookService.update(uw);

        //2.然后将当前的设置的默认地址的is_default设置为1
        addressBook.setIsDefault(1);
        boolean flag = addressBookService.updateById(addressBook);
        return flag ? R.success("默认地址变更成功") : R.error("默认地址变更失败");
    }

    /**
     * 新增
     *
     * @param addressBook
     * @return R<String>
     */
    @PostMapping
    public R<String> insertAddressBook(@RequestBody AddressBook addressBook) {
        //获取当前登录的用户的user_id
        Long userId = BaseContext.getCurrentId();
        //调用业务层的添加方法,此时addressBook对象还缺少id
        addressBook.setUserId(userId);
        boolean save = addressBookService.save(addressBook);
        return save ? R.success("地址簿添加成功") : R.error("地址簿添加失败");
    }

    /**
     * 删除
     *
     * @param ids
     * @return R<String>
     */
    @DeleteMapping("/{ids}")
    public R<String> removeAddressBook(@PathVariable Long ids) {
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(ids != null, AddressBook::getId, ids);
        addressBookService.remove(qw);
        return R.success("地址删除成功");
    }

    /**
     * 修改
     *
     * @param addressBook
     * @return R<String>
     */
    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook) {
        boolean flag = addressBookService.updateById(addressBook);
        return flag ? R.success("地址修改成功") : R.error("地址修改失败");
    }

    /**
     * (数据回显)修改地址簿页面
     *
     * @param id
     * @return R<AddressBook>
     */
    @GetMapping("/{id}")
    public R<AddressBook> getAddressBookById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /**
     * 查询所有
     *
     * @return R<List < AddressBook>>
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(HttpServletRequest request) {
        log.info("当前登录的用户Id为" + BaseContext.getCurrentId());
        log.info("(Session)当前登录的用户Id为: " + request.getSession().getAttribute("user"));
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);
    }

    /**
     * (数据回显)默认地址信息
     *
     * @return R<AddressBook>
     */
    @GetMapping("/default")
    public R<AddressBook> getDefaultAddressBookById() {
        log.info("当前登录的用户Id为" + BaseContext.getCurrentId());
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        qw.eq(AddressBook::getIsDefault, 1);
        AddressBook addressBook = addressBookService.getOne(qw);
        return R.success(addressBook);
    }
}
